package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringFeedBackReplyForTeamView;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringFeedBackReplyForTeamPresenter;
import com.cloudsigns.factorymanager2.event.EngineeringOrderDetailForTeamEvent;
import com.cloudsigns.factorymanager2.project.customview.GeneralPurposePoupWindow;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by tony on 2018/6/9.
 */

public class EngineeringInstallFeedBackReplyForTeamActivity extends Activity implements View.OnClickListener, IEngineeringFeedBackReplyForTeamView, ItemClickListener {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private EngineeringDailyFeedBackBean engineeringDailyFeedBackBean;
    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    private TextView edt_feed_back_type, edt_damageProduct, edt_advice, edt_feedbackDate, edt_reply_content_view, edt_confirm_person, edt_confirm_time;
    private TextView tv_damageProduct;
    private View rl_replay_wait;
    private View rl_reply_content_view, rl_confirm_person, rl_confirm_time;
    private EditText edt_reply_wait;
    private TextView btn_submit;
    private ImageView img_statu_feedback_right;
    private EngineeringFeedBackReplyForTeamPresenter engineeringFeedBackReplyForTeamPresenter;

    private GeneralPurposePoupWindow generalPurposePoupWindow;
    private String datas[] = {"制作完成", "修补完成", "购买完成", "重新发货已经到达", "施工协调已经到位", "文件调整已经清晰"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_engineeringisntall_feedback_repaly_for_team);

        engineeringFeedBackReplyForTeamPresenter = new EngineeringFeedBackReplyForTeamPresenter(this);
        init();
        Intent mIntent = getIntent();
        if (null != mIntent) {
            engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
            engineeringInstallationOrderBean = (EngineeringInstallationOrderBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
            initData();
        }


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        edt_feed_back_type = findViewById(R.id.edt_feed_back_type);
        edt_damageProduct = findViewById(R.id.edt_damageProduct);
        edt_advice = findViewById(R.id.edt_advice);
        edt_feedbackDate = findViewById(R.id.edt_feedbackDate);
        edt_reply_content_view = findViewById(R.id.edt_reply_content_view);
        edt_confirm_person = findViewById(R.id.edt_confirm_person);
        edt_confirm_time = findViewById(R.id.edt_confirm_time);
        edt_reply_wait = findViewById(R.id.edt_reply_wait);
        rl_replay_wait = findViewById(R.id.rl_replay_wait);
        rl_reply_content_view = findViewById(R.id.rl_reply_content_view);
        rl_confirm_person = findViewById(R.id.rl_confirm_person);
        rl_confirm_time = findViewById(R.id.rl_confirm_time);
        btn_submit = findViewById(R.id.btn_submit);
        img_statu_feedback_right = findViewById(R.id.img_statu_feedback_right);
        tv_damageProduct = findViewById(R.id.tv_damageProduct);

        tv_title.setText("异常回复");


        rl_back.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        img_statu_feedback_right.setOnClickListener(this);
    }

    private void initData() {
        if (null != engineeringDailyFeedBackBean) {

            CommonEnum.FeedBackType type = engineeringDailyFeedBackBean.getType();
            if (null != type) {
                if (type == CommonEnum.FeedBackType.NORMAL) {
                    tv_damageProduct.setText("正常反馈");
                } else if (type == CommonEnum.FeedBackType.UNUSUAL) {
                    CommonEnum.SubType subype = engineeringDailyFeedBackBean.getSubType();
                    if (null != type) {
                        if (subype == CommonEnum.SubType.DAMAGE) {
                            tv_damageProduct.setText("物流货损");
                        } else if (subype == CommonEnum.SubType.DELAY) {
                            tv_damageProduct.setText("物流延误");
                        } else if (subype == CommonEnum.SubType.INSTALL_UNUSUAL) {
                            tv_damageProduct.setText("安装异常");
                        } else if (subype == CommonEnum.SubType.OMIT) {
                            tv_damageProduct.setText("物流漏货");
                        }
                    }

                } else if (type == CommonEnum.FeedBackType.COMPLETED) {
                    tv_damageProduct.setText("完工反馈");
                }

            }

            String advice = engineeringDailyFeedBackBean.getAdvice();
            if (null != advice && !TextUtils.isEmpty(advice) && !"null".equals(advice))
                edt_advice.setText(engineeringDailyFeedBackBean.getAdvice());

            edt_feedbackDate.setText(DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getFeedbackDate()));

            CommonEnum.UnusualStatus unusualStatus = engineeringDailyFeedBackBean.getUnusualStatus();
            if (null != unusualStatus) {
                if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY) {
                    rl_replay_wait.setVisibility(View.VISIBLE);
                    rl_reply_content_view.setVisibility(View.GONE);

                    rl_confirm_person.setVisibility(View.GONE);
                    rl_confirm_time.setVisibility(View.GONE);
                    btn_submit.setVisibility(View.VISIBLE);
                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM) {
                    rl_replay_wait.setVisibility(View.GONE);
                    rl_reply_content_view.setVisibility(View.VISIBLE);

                    rl_confirm_person.setVisibility(View.GONE);
                    rl_confirm_time.setVisibility(View.GONE);
                    btn_submit.setVisibility(View.GONE);

                    edt_reply_content_view.setText(engineeringDailyFeedBackBean.getCompleteUnusualDesc());

                } else if (unusualStatus == CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH) {
                    rl_replay_wait.setVisibility(View.GONE);
                    rl_reply_content_view.setVisibility(View.VISIBLE);

                    rl_confirm_person.setVisibility(View.VISIBLE);
                    rl_confirm_time.setVisibility(View.VISIBLE);
                    btn_submit.setVisibility(View.GONE);

                    edt_confirm_person.setText(engineeringDailyFeedBackBean.getConfirmCompleteUnusualAccountName() + ":" + engineeringDailyFeedBackBean.getConfirmCompleteUnusualAccountId());
                    edt_confirm_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getConfirmCompleteUnusualDate()));
                    edt_reply_content_view.setText(engineeringDailyFeedBackBean.getCompleteUnusualDesc());

                }

            }
        }
    }

    private void repaly() {
        engineeringFeedBackReplyForTeamPresenter.engineeringFeedBackRply();
    }


    private void showMaintanceTypesWindow(View anchor) {
        generalPurposePoupWindow = new GeneralPurposePoupWindow(this, datas);
        generalPurposePoupWindow.setItemClick(this);
        generalPurposePoupWindow.showPopupWindow(anchor);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_submit:
                repaly();
                break;
            // 回复内容
            case R.id.img_statu_feedback_right:
                showMaintanceTypesWindow(edt_reply_wait);
                break;
        }

    }

    @Override
    public String getContent() {
        return edt_reply_wait.getText().toString().trim();
    }

    @Override
    public String getDailyFeedbackId() {
        if (null != engineeringDailyFeedBackBean)
            return engineeringDailyFeedBackBean.getId();
        return null;
    }

    @Override
    public String getOrderId() {
        if (null != engineeringInstallationOrderBean)
            return engineeringInstallationOrderBean.getId();
        return null;
    }

    @Override
    public CommonEnum.EngineeringORderCategory getType() {

        return CommonEnum.EngineeringORderCategory.INSTALL;
    }

    @Override
    public void engineeringFeedBackRplySuccessed() {
        Toast.makeText(this, "回复成功", Toast.LENGTH_SHORT).show();
        EventBus.getDefault().post(new EngineeringOrderDetailForTeamEvent());
        finish();
    }

    @Override
    public void engineeringFeedBackRplyFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void itemClick(View view, int position) {
        edt_reply_wait.setText(datas[position]);
    }
}
